package cn.scy.util.xiamdx;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import cn.scy.constant.BusinessConst;
import cn.scy.exception.BusinessException;
import com.alibaba.fastjson.JSONObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.UUID;

public class TestNumberAuth {


    public static void main(String[] args) throws Exception {
        String appId = "00007210140118";
        String appSecect = "a023d95b2fd66954aa0dc398";
        String photo = encodeFileToBase64("D:/personal/557.png");
        String requestId = UUID.randomUUID().toString().replace("-", "");
        Long timeStamp = System.currentTimeMillis() / 1000;

        RealNameAuthRequestDto dto = RealNameAuthRequestDto.builder()
                .name(AESGCMUtil.encrypt("刘玉红", appSecect))
                .appId(appId)
                .photo(AESGCMUtil.encrypt(photo,appSecect))
                .identifyCardNO(AESGCMUtil.encrypt("320623198704292223", appSecect))
                .requestId(requestId)
                .timestamp(timeStamp)
                .certificateType("1")
                .phoneNo(AESGCMUtil.encrypt("16744481765", appSecect))
                .build();
        String jsonData = JSONUtil.toJsonStr(dto);
        System.out.println("入参"+jsonData);
        String signature = MD5.makeMD5(String.valueOf(timeStamp),appSecect,jsonData);
        String Auth = generateAuthorization(appId,String.valueOf(timeStamp),signature);
        System.out.println("Authorization"+Auth);
        try{
            HttpResponse response = HttpRequest.post("http://182.43.124.54:30001/svc/col/csServer/cmreal/jk/creNumberAuth")
            //HttpResponse response = HttpRequest.post("http://127.0.0.1:31000/cmreal/jk/creNumberAuth")

                    .header("Authorization", Auth)  // 设置 Authorization 头
                    .header("Content-Type", "application/json")   // 设置 Content-Type
                    .body(jsonData)  // 设置请求体
                    .timeout(3000000)  // 设置超时时间，单位毫秒
                    .execute();
            System.out.println("出参"+response.getStatus()+"="+response.body());
            if(response.getStatus() == 200){
                System.out.println(response.body());
            }

            // 解析响应
            JSONObject postResultJson = JSONObject.parseObject(response.body());
            System.out.println("postResultJson="+postResultJson);
            if (ObjectUtil.equals(200, response.getStatus())) {
                System.out.println(postResultJson.getString("resultMsg"));
            }
            /*
            出参200={"resultCode":"0","resultMsg":"请求成功"}
            {"resultCode":"0","resultMsg":"请求成功"}
            postResultJson={"resultCode":"0","resultMsg":"请求成功"}
            */

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static String generateAuthorization(String appId,String timestamp, String signature) {
        // 构造Authorization头
        return "EOPAUTH appId=\"" + appId + "\",timestamp=\"" + timestamp + "\",signature=\"" + signature + "\"";
    }

    // 将文件转换为 Base64 编码字符串
    public static String encodeFileToBase64(String filePath) throws IOException {
        // 创建文件对象
        File file = new File(filePath);
        // 获取文件字节流
        FileInputStream fileInputStream = new FileInputStream(file);
        byte[] fileBytes = new byte[(int) file.length()];

        // 读取文件内容
        fileInputStream.read(fileBytes);
        fileInputStream.close();
        // 使用 Base64 编码文件字节数组
        return Base64.getEncoder().encodeToString(fileBytes);
    }
}
